package com.dhcc.bpm.modules.scheduler.component;


import com.dhcc.bpm.common.redisMessage.RedisLock;
import com.dhcc.bpm.common.redisMessage.RedisService;
import com.dhcc.bpm.common.utils.IpAddressUtil;
import com.dhcc.bpm.common.utils.OpenapiGeneratorUtils;
import com.dhcc.bpm.modules.scheduler.service.ReportService;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.Calendar;

/**
 * 报表统计，定时推送
 */
@Component("schedulerComponent")
@Slf4j
@AllArgsConstructor
@NoArgsConstructor
public class SchedulerTaskComponent {

    @Autowired
    private ReportService reportService;
    @Autowired
    OpenapiGeneratorUtils openapiGeneratorUtils;

    @Resource(name = "common.RedisService")
    private RedisService redisService;

    @Value("${scheduler.report.day}")
    private String dayKey;

    @Value("${scheduler.report.week}")
    private String weekKey;

    @Value("${scheduler.report.month}")
    private String monthKey;
    @Value("${scheduler.report.isFlag}")
    private Boolean isFlag;

    /**
     * 日报推送：每日下午17点
     */
    @Scheduled(cron = "0 0 17 * * ?")
    public void dayReport(){
        if(!isFlag){
            return;
        }
        RedisLock redisLock = new RedisLock(redisService, dayKey, 40L);
        try {
            if (redisLock.lock()) {
                log.info("执行当前进程的ip为：{}", IpAddressUtil.getIpAddress());
                log.info("+++++++++++日报发送开始++++++++++++++");
                reportService.dayReport();
            }
        }catch (Exception e){
            log.error(e.getMessage(), "日报发送异常"+ e);
        }finally {
            redisLock.unlock();
        }
    }

    /**
     * 周报推送：每周周日下午17点
     */
    @Scheduled(cron = "0 0 17 ? * 1")
    public void weekReport(){
        if(!isFlag){
            return;
        }
        RedisLock redisLock = new RedisLock(redisService, weekKey, 40L);
        try {
            if (redisLock.lock()) {
                log.info("执行当前进程的ip为：{}", IpAddressUtil.getIpAddress());
                log.info("+++++++++++周报发送开始++++++++++++++");
                reportService.weekReport();
            }
        }catch (Exception e){
            log.error(e.getMessage(), "周报发送异常"+ e);
        }finally {
            redisLock.unlock();
        }
    }

    /**
     * 月报推送：每月最后一天下午17点
     */
    /*@Scheduled(cron = "0 0 17 28 * ?")
    public void monthReport(){
        reportService.monthReport();
    }*/

    @Scheduled(cron = "0 15 10 28-31 * ?")
    public void execute() {
        if(!isFlag){
            return;
        }
        final Calendar c = Calendar.getInstance();
        if (c.get(Calendar.DATE) == c.getActualMaximum(Calendar.DATE)) {
            //是最后一天
            RedisLock redisLock = new RedisLock(redisService, monthKey, 40L);
            try {
                if (redisLock.lock()) {
                    log.info("执行当前进程的ip为：{}", IpAddressUtil.getIpAddress());
                    log.info("+++++++++++月报发送开始++++++++++++++");
                    reportService.monthReport();
                }
            }catch (Exception e){
                log.error(e.getMessage(), "月报发送异常"+ e);
            }finally {
                redisLock.unlock();
            }
        }
    }

    @Scheduled(cron = "0 0 23 * * ?")
    public void clearShellLog() {
        if(!isFlag){
            return;
        }
        Long time = System.currentTimeMillis()-1000*60*60*24;
        File listFiles  = new File(openapiGeneratorUtils.getPath()+"/log");
        if(listFiles != null){
            File[] fs = listFiles.listFiles();
            for (File f : fs){
                try {
                    if(Long.parseLong(f.getName().substring(0,13))<time){
                        log.info("删除的日志名称为{}",f.getName());
                        f.delete();
                    }
                }catch (Exception e){
                    log.error("文件名称不符合规则,删除失败",e);
                }
            }
        }


    }
}
